using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for HoaDonBO
/// </summary>
public class OrderBO
{
    public OrderBO()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public DataTable OrderGetAll()
    {
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return null;
        DataTable result = new DataTable();
        string sql = "SELECT T1.OrderId, T1.CustomerId, T1.OrderDate, T1.DateSend, T1.Name, T1.Address, T1.TotalAmount, T2.MenuId, T2.FoodId, T2.Quantity, T1.PayType";
        sql += " FROM [OFSSDB].[dbo].[Order] T1";
        sql += " LEFT JOIN [OFSSDB].[dbo].[OrderDetails] T2 ON T1.OrderId = T2.OrderId";
        result = connect.GetDataTable(sql);
        connect.Close();
        return result;
    }

    public DataTable GetFinishedOrderInDay()
    {
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return null;
        DataTable result = new DataTable();
        string sql = "SELECT T1.OrderId, T1.CustomerId, T1.OrderDate, T1.DateSend, T1.Name, T1.Address, T1.TotalAmount";
        sql += " FROM [OFSSDB].[dbo].[Order] T1 where T1.IsApprove = 1 AND convert(varchar,T1.DateSend,102)= convert(varchar,getdate(),102)";
        result = connect.GetDataTable(sql);
        connect.Close();
        return result;
    }

    public DataTable GetOrderNotSend()
    {
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return null;
        DataTable result = new DataTable();
        string sql = "SELECT T1.OrderId, T1.CustomerId, T1.OrderDate, T1.DateSend, T1.Name, T1.Address, T1.TotalAmount";
        sql += " FROM [OFSSDB].[dbo].[Order] T1 where T1.IsApprove = 0 AND convert(varchar,T1.DateSend,102) = NULL";
        result = connect.GetDataTable(sql);
        connect.Close();
        return result;
    }

    public DataTable GetOrderNotPay()
    {
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return null;
        DataTable result = new DataTable();
        string sql = "SELECT T1.OrderId, T1.CustomerId, T1.OrderDate, T1.DateSend, T1.Name, T1.Address, T1.TotalAmount";
        sql += " FROM [OFSSDB].[dbo].[Order] T1 where T1.IsApprove = 0 AND T1.TotalAmount <= 0";
        result = connect.GetDataTable(sql);
        connect.Close();
        return result;
    }

    public DataTable GetOrderNotComplete()
    {
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return null;
        DataTable result = new DataTable();
        string sql = "SELECT T1.OrderId, T1.CustomerId, T1.OrderDate, T1.DateSend, T1.Name, T1.Address, T1.TotalAmount";
        sql += " FROM [OFSSDB].[dbo].[Order] T1 where T1.IsApprove = 0 AND (T1.TotalAmount <= 0 OR convert(varchar,T1.DateSend,102) = NULL)";
        result = connect.GetDataTable(sql);
        connect.Close();
        return result;
    }

    public DataTable GetFinishedOrder()
    {
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return null;
        DataTable result = new DataTable();
        string sql = "SELECT T1.OrderId, T1.CustomerId, T1.OrderDate, T1.DateSend, T1.Name, T1.Address, T1.TotalAmount";
        sql += " FROM [OFSSDB].[dbo].[Order] T1 where T1.IsApprove = 1 AND T1.TotalAmount > 0";
        result = connect.GetDataTable(sql);
        connect.Close();
        return result;
    }

    public DataTable GetOrderById(string id)
    {
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return null;
        DataTable result = new DataTable();
        string sql = "SELECT T1.OrderId, T1.CustomerId, T1.OrderDate, T1.DateSend, T1.Name, T1.Address, T1.TotalAmount";
        sql += " FROM [OFSSDB].[dbo].[Order] T1 where T1.OrderId = " + id;
        result = connect.GetDataTable(sql);
        connect.Close();
        return result;
    }

    public bool OrderInsert(OrderEntity entity)
    {
        bool result = false;
        DBconnect connect = new DBconnect();
        connect.Open();
        if (connect.IsConnected == false)
            return false;
        string[] listcol = new string[] { "CustomerId", "OrderDate", "DateSend", "PromotionId", "Address", "Name", "TotalAmount", "PayType", "OrderTypeId", "IsApprove", "IsDelete" };
        object[] listvalue = new object[] { entity.CustomerId, entity.OrderDate, entity.DateSend, entity.PromotionId, entity.Address, entity.Name, entity.TotalAmount, entity.PayType, entity.OrderTypeId, 0, 0 };
        int i = connect.Insert("[OFSSDB].[dbo].[Order]", listcol, listvalue);

        if (i > 0)
            result = true;

        connect.Close();
        return result;
    }
}
